package com.huanxing.cloud.auth.handler;

import com.huanxing.cloud.common.security.entity.HuanxingUser;
import com.huanxing.cloud.common.security.hander.AbstractAuthenticationSuccessEventHandler;
import com.huanxing.cloud.common.security.utils.SecurityUtils;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.Authentication;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.Objects;

/** @description 认证成功处理 @Author lijx @Date 2022/1/27 9:40 */
@Slf4j
@Component
@AllArgsConstructor
public class AuthenticationSuccessEventHandler extends AbstractAuthenticationSuccessEventHandler {

  @Override
  public void handle(Authentication authentication) {
    HuanxingUser huanxingUser = SecurityUtils.getUser(authentication);
    HttpServletRequest request =
        ((ServletRequestAttributes)
                Objects.requireNonNull(RequestContextHolder.getRequestAttributes()))
            .getRequest();
    String url = request.getRequestURI();
    log.info("用户：{} 授权成功，url：{}", huanxingUser.getUsername(), url);
  }
}
